package tables

import (
	"gorm.io/gorm"
	"time"
)

type UserLogin struct {
	ID        int32  `gorm:"primary_key;type:int"`
	UserID    int32  `gorm:"column:user_id;type:int;not null;index;"`
	LoginJSON JSON   `gorm:"type:JSON"`
	Date      string `gorm:"type:varchar(4)"`

	CreatedAt *MyTime `gorm:"column:create_time;"`
	UpdatedAt *MyTime `gorm:"column:update_time;"`
}

func (u *UserLogin) TableName() string {
	return "user_login"
}

// 钩子函数
func (u *UserLogin) BeforeCreate(tx *gorm.DB) error {
	t := time.Now().UTC().Format(FormatTime)
	fptTime, err := time.ParseInLocation(FormatTime, t, time.Local)
	if err != nil {
		return err
	}
	u.CreatedAt = &MyTime{Time: fptTime}
	u.UpdatedAt = &MyTime{Time: fptTime}
	return nil
}

func (u *UserLogin) BeforeUpdate(scope *gorm.DB) error {
	t := time.Now().UTC().Format(FormatTime)
	fptTime, err := time.ParseInLocation(FormatTime, t, time.Local)
	if err != nil {
		return err
	}
	u.UpdatedAt = &MyTime{Time: fptTime}
	return nil
}
